﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class loaiTBSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public loaiTBSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public List<_listJsontoTree> getListTree()
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();
                var _listMVT = dbContext.MANGVT.ToList();
                foreach (var item in _listMVT)
                {
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = item.MA;
                    itemMVT.text = item.TEN;
                    itemMVT.type = "MVT";
                    itemMVT.Idparent = item.MA;
                   var listChild = dbContext.LOAI_TBI.Where(p => p.MANGVT == item.MA).ToList();
                   foreach (var itemloaiTb in listChild)
                   {
                       _listJsontoTree itemLoaiTb = new _listJsontoTree();
                       itemLoaiTb.id = itemloaiTb.MA.ToString();
                       itemLoaiTb.text = itemloaiTb.TEN;
                       itemLoaiTb.type = "LOAITB";
                       itemLoaiTb.Idparent = item.MA;
                       itemMVT.children.Add(itemLoaiTb);
                   }
                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }

        public List<LOAI_TBI> getList(string mangVTId)
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                return dbContext.LOAI_TBI.Where(p => p.MANGVT == mangVTId || mangVTId == "").ToList();
            }
            catch (Exception e)
            {
                List<LOAI_TBI> _listRe = new List<LOAI_TBI>();
                return _listRe;
            }
        }

        public LOAI_TBI getByMa(decimal ma)
        {
            try
            {
                return dbContext.LOAI_TBI.Where(p => p.MA == ma).First();
            }
            catch (Exception e) { LOAI_TBI _en = new LOAI_TBI(); return _en; }
        }

        public List<LOAI_TBI> getALLMangVT(string maVt)
        {
            try
            {
                return dbContext.LOAI_TBI.Where(p => p.MANGVT == maVt).ToList();
            }
            catch (Exception e) { List<LOAI_TBI> _listReturn = new List<LOAI_TBI>(); return _listReturn; }
        }
    }
}
